package map.android.baidu.carpool.data.impl;

import android.content.Context;
import android.text.TextUtils;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import map.android.baidu.carpool.WebConfig;
import map.android.baidu.carpool.data.CarpoolBaseData;
import map.android.baidu.carpool.data.CarpoolRequestParam;
import map.android.baidu.carpool.model.LocalePlaceInfo;
import map.android.baidu.carpool.response.OrderCreateResponse;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangtongyu on 15/3/24.
 * main模块创建订单接口 request
 */
public class OrderCreateData extends CarpoolBaseData<OrderCreateResponse>{

    private LocalePlaceInfo startPlaceInfo;
    private LocalePlaceInfo arrivePlaceInfo;

    private String startTime;
   // private String taxiPrice = "";
    private String selectCarType;
    private String selectCarPrice;
    private String bduss;
    private String cuid;
    private GeoPoint currentPlaceGeo;
    private String remark = "";
    private String distance = "";
    //private String sign = "";
    private String currentCityId;
    private String nickName=""; //乘客姓氏
    private int sex = 0; //乘客性别


    public OrderCreateData(Context context) {
        super(context);
    }

    public void setStartTime(String startTime){
        this.startTime = startTime;
    }

    public void setBduss(String bduss) {
        this.bduss = bduss;
    }

    public void setArrivePlaceInfo(LocalePlaceInfo arrivePlaceInfo) {
        this.arrivePlaceInfo = arrivePlaceInfo;
    }

    public void setStartPlaceInfo(LocalePlaceInfo startPlaceInfo){
        this.startPlaceInfo = startPlaceInfo;

    }

    public void setSelectCarType(String selectCarType){
        this.selectCarType = selectCarType;

    }

    public void setCurrentCityId(String currentCityId) {
        this.currentCityId = currentCityId;
    }

    public void setCurrentPlaceGeo(GeoPoint currentPlaceGeo) {
        this.currentPlaceGeo = currentPlaceGeo;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    public void setSex(int sex){
        this.sex = sex;
    }

    @Override
    protected String getUrl() {
        return WebConfig.getUrl(WebConfig.URL_ID_ORDER_CREATE);
    }

    @Override
    protected Map<String, String> getParams() {
        Map<String , String> params = new HashMap<String, String>();
        params.put("startPlaceInfo" , transLocalePlaceInfoToJsonObj(startPlaceInfo , 1).toString());
        params.put("arrivePlaceInfo" , transLocalePlaceInfoToJsonObj(arrivePlaceInfo , 2).toString());
        params.put("startTime" , startTime);
       // params.put("taxiPrice" , taxiPrice);
        params.put("selectCarType" , selectCarType);
       // params.put("selectCarPrice" , selectCarPrice);
        params.put("bduss" , bduss);
        if (currentPlaceGeo != null){
            params.put("currentPlaceGeo" , currentPlaceGeo.getLongitude() + "," + currentPlaceGeo.getLatitude());
        }
        params.put("remark" , remark);
        params.put("distance" , distance);
        if(sex != 0){
            params.put("gender",sex+"");
        }
        if (!TextUtils.isEmpty(nickName)){
            params.put("nickname",nickName);
        }

        String sign = constructNeedSignParams();

        params.put("sign", sign);

        return params;
    }

    private String constructNeedSignParams() {
        Map<String , String> needSignParam = new HashMap<String, String>();

        needSignParam.put("startPlaceInfo" , transLocalePlaceInfoToJsonObj(startPlaceInfo , 1).toString());
        needSignParam.put("arrivePlaceInfo" , transLocalePlaceInfoToJsonObj(arrivePlaceInfo , 2).toString());
        needSignParam.put("startTime" , startTime);
        needSignParam.put("selectCarType" , selectCarType);
        if (currentPlaceGeo != null){
            needSignParam.put("currentPlaceGeo" , currentPlaceGeo.getLongitude() + "," + currentPlaceGeo.getLatitude());
        }
        needSignParam.put("token", "baidu2015_carpo0420_^&*");

        return CarpoolRequestParam.signPartOfParams(needSignParam);
    }

    /**
     *提供将LocalePlaceInfo类转换成Json的方法
     * @param localePlaceInfo 转换原料
     *        startOrArrive   出发地或目的地 value：1：start OR 2：arrive
     */
    private JSONObject transLocalePlaceInfoToJsonObj(LocalePlaceInfo localePlaceInfo , int startOrArrive){
        JSONObject jsonObject = new JSONObject();

        switch (startOrArrive) {
            case 1:
                try {
                    jsonObject.put("startPlaceGeo", String.valueOf(localePlaceInfo.placeGeo.getLongitude()) + "," + String.valueOf(localePlaceInfo.placeGeo.getLatitude()));
                    jsonObject.put("startPlace", localePlaceInfo.placeName);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case 2:
                try {
                    jsonObject.put("arrivePlaceGeo", String.valueOf(localePlaceInfo.placeGeo.getLongitude()) + "," + String.valueOf(localePlaceInfo.placeGeo.getLatitude()));
                    jsonObject.put("arrivePlace", localePlaceInfo.placeName);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }

        return jsonObject;
    }
}
